import requests
import re
import json
import pymysql
import hashlib
import datetime

class GLY(object):
    def __init__(self):
        self.url = 'https://fe-api.zhaopin.com/c/i/sou?start=0&pageSize=100&cityId=530&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=python&kt=3&_v=0.17488384&x-zp-page-request-id=718d1438509748c89fce1de8a09ae222-1544929858952-572848'

        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
        }
        self.links = []
        self.host = '127.0.0.1'
        self.db = 'app_mark'
        self.password = '123456'
        self.user = 'root'
        self.charset = 'utf8mb4'
        # 直接修改kw可修改搜索关键字
        self.kw = 'python'
        # 翻页参数，暂不修改
        self.start = '0'
        
    def get_json(self):
        data = requests.get(self.url)
        data = data.text
        return data

    def parse_data(self, data):
        data = json.loads(data)
        results = data['data']['results']
        for result in results:
            result = str(result)
            link = re.findall("positionURL': '(.*?)'", result, re.S)[0]
            link = str(link)
            hkey = hashlib.md5((link+self.kw).encode('utf-8')).hexdigest()
            tag = '0'
            sitename = '智联招聘'
            lasttime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            list_data = [link, hkey, tag, sitename, lasttime]
            self.save_data(list_data)
            print(link)

    def save_data(self, list_data):
        con = pymysql.connect(host=self.host, db=self.db, user=self.user, passwd=self.password, charset=self.charset)
        cur = con.cursor()
        sql = 'insert into gly(link, hkey, tag, sitename, lasttime) values (%s, %s, %s, %s, %s)'
        try:
            cur.execute(sql, list_data)
            print('insert success')
        except Exception as e:
            con.rollback()
            print('failed~~', e)
        else:
            con.commit()
        cur.close()
        con.close()


if __name__ == '__main__':
    gly = GLY()
    data = gly.get_json()
    gly.parse_data(data)